package com.sina.shop.manage.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.concurrent.TimeUnit;

/**
 * created  on 13/06/2017.
 */
@Configuration
public class WebCustomConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/css/**")
                .addResourceLocations("classpath:/static/css/")
                .setCacheControl(CacheControl.noStore());

        registry.addResourceHandler("/js/**")
                .addResourceLocations("classpath:/static/js/")
                .setCacheControl(CacheControl.noStore());

        registry.addResourceHandler( "/img/**")
                .addResourceLocations("classpath:/static/img/")
                .setCacheControl(CacheControl.noStore().cachePrivate());

        registry.addResourceHandler("/view/**")
                .addResourceLocations("classpath:/static/view/")
                .setCacheControl(CacheControl.noStore().cachePrivate());

        registry.addResourceHandler("/lib/**")
                .addResourceLocations("classpath:/static/lib/")
                .setCacheControl(CacheControl.maxAge(30, TimeUnit.DAYS));


    }


    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorParameter(true)
                .defaultContentType(MediaType.APPLICATION_JSON_UTF8)
                .favorPathExtension(false)
                .parameterName("content")
                .ignoreAcceptHeader(true)
        ;

    }
}
